<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select v-model="listQuery.brandId" clearable size="small" class="filter-item" style="width: 200px;"
                 placeholder="请选择所属门店"
      >
        <el-option v-for="item in brandList" :key="item.id" :label="item.name" :value="item.id"/>
      </el-select>
        <el-input
          v-model="listQuery.goodsSn"
          clearable
          size="mini"
          class="filter-item"
          style="width: 200px;"
          placeholder="请输入商品编号"
        />
        <el-input
          v-model="listQuery.barCode"
          clearable
          size="mini"
          class="filter-item"
          style="width: 200px;"
          placeholder="请输入商品条形码"
        />
        <el-input
          v-model="listQuery.goodsName"
          clearable
          size="mini"
          class="filter-item"
          style="width: 200px;"
          placeholder="请输入商品名称"
        />
        <el-button
          size="mini"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >查找
        </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        align="center"
        min-width="110"
        label="门店名称"
        prop="brandName"
      />
      <el-table-column
        align="center"
        min-width="110"
        label="条形码"
        prop="barCode"
      />
      <el-table-column
        align="center"
        min-width="110"
        label="商品编号"
        prop="goodsSn"
      />
      <el-table-column
        align="center"
        min-width="200"
        label="名称"
        prop="goodsName"
      />

      <el-table-column
        align="center"
        property="picUrl"
        label="图片"
      >
        <template slot-scope="scope">
          <img
            :src="scope.row.picUrl"
            width="40"
          >
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="商品规格"
        prop="specifications"
      />

      <el-table-column
        align="center"
        label="库存数量"
        prop="inventoryNumber"
      />

      <el-table-column
        align="center"
        label="库存成本"
        prop="inventoryCost"
      />

      <el-table-column
        align="center"
        label="赠品"
        prop="giftWare"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.giftWare"
            active-color="#ff4949"
            inactive-color="#13ce66"
            :active-value="1"
            :inactive-value="0"
            :disabled="true"
          />
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-tooltip
      placement="top"
      content="返回顶部"
    >
      <back-to-top :visibility-height="100"/>
    </el-tooltip>

  </div>
</template>

<script>
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { getToken } from '@/utils/auth'
import { listInventory } from '@/api/business/inventory'
import { listBrand } from '@/api/business/brand'

export default {
  name: 'InventoryList',
  components: { BackToTop, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        goodsSn: '',
        productId: undefined,
        goodsName: undefined,
        sort: 'add_time',
        order: 'desc',
        barCode: '',
        brandId: ''
      },
      brandList: []
    }
  },
  computed: {
    headers() {
      return {
        'X-Dts-Merchant-Token': getToken()
      }
    }
  },
  created() {
    this.getBrandList()
  },
  methods: {
    getBrandList() {
      listBrand().then(response => {
        let data = response.data.data.items
        if (data.length > 0) {
          data.forEach(item => {
            if (item.id != 1001000) {
              this.brandList.push(item)
            }
          })
          if (this.brandList.length > 0) {
            this.listQuery.brandId = this.brandList[0].id
          }
          this.getList()
        }
      }).catch((res) => {
        this.brandList = []
        console.log(res)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      listInventory(this.listQuery).then(response => {
        this.list = response.data.data.items
        this.total = response.data.data.total
        this.isAdmin = response.data.data.isAdmin
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    }
  }
}
</script>

<style>
.el-dialog {
  width: 60%;
}

.table-expand {
  font-size: 0;
}

.table-expand label {
  width: 100px;
  color: #99a9bf;
}

.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}

.gallery {
  width: 80px;
  margin-right: 10px;
}

.htmlSpan {
  white-space: pre-wrap;
  word-break: break-all;
}
</style>
